<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo
*
* @package		Ineo
* @author		Dan Cryer & William McGann
* @copyright	Dan Cryer & William McGann
* @version		2.x
*/
class ineo
{
	/**
	* Setup Variables:
	*/
	var $ipsclass     = null;    // ip.board ipsclass
	var $api          = null;    // the ineo component file ( components_public/ineo.php )
	var $modules      = array(); // all loaded ineo modules ( usually only one )
	var $libs         = array(); // all loaded ineo libraries
	var $version      = null;    // current version info
	var $base_url     = '';      // base url to ineo ( http://domain.com/index.php?autocom=ineo& )
	var $menu         = '';      // any additions to the global menu by other parts of the script.
	var $sidebar      = '';      // any additions to go below the global menu in the sidebar.

	/**
	* Initialization
	*/
	function ineo( &$component )
	{
		// Set up our variables:
		$this->api        =& $component;
		$this->ipsclass   =& $component->ipsclass;
		$this->root_path  =& $component->root_path;
		$this->sys_path   =& $this->root_path;
		$this->base_url   =& $component->base_url;

		// Load the general, events and permissions global libraries:
		$this->load_library('global');
		$this->load_library('events');
		$this->load_library('permissions');
		
		// Added 1.1 - Driver-Specific SQL library.
		$this->load_library( $this->ipsclass->vars['sql_driver'] );
		$this->dblib =& $this->libs[$this->ipsclass->vars['sql_driver']];

		// Require Version File:
		require_once( $this->root_path.'version.php' );
		$this->version = $version;
	}

	/**
	* User-Side Run ( called -after- ipsclass, etc. are passed ).
	*/
	function run()
	{
		//die('<pre>'.print_r($this->ipsclass->vars,true).'</pre>');
		//$this->load_library('categories');
		//die('<pre>'.print_r($this->ipsclass->cache['ineo_cats'],true));
		$this->templates    =& $this->ipsclass->compiled_templates;
		
		// Load global templates:
		$this->load_template('global');

		// Load language file
		$this->ipsclass->load_language('lang_ineo');
		
		// Set up title and nav:
		$this->title = $this->ipsclass->vars['board_name'] . ' - ' . $this->ipsclass->vars['ineo_page_title'];
		
		// Build Breadcrumb Navigation:
		$this->libs['global']->build_nav();
		$this->convert_op_shortcuts();

		if ( $this->permissions->global_check() == true )
		{
			$this->op_handle();
		}
		else
		{
			$string = $this->ipsclass->lang['ineo_perm_error_text'];
						
			if( !empty( $this->ipsclass->lang[$this->permission_error] ) )
			{
				$string = $this->ipsclass->lang[$this->permission_error];
			}
			
			$this->global->error($this->ipsclass->lang['ineo_perm_error'],$string);
		}

		$this->do_output();
	}

	/**
	* Load library file ( stored in /lib )
	*
	* @todo Add error message 'error_load_library'
	*/
	function load_library( $library_name )
	{
		$library_name = preg_replace('/([^a-zA-Z0-9_]+)/','',$library_name);

		// Have we loaded it already?
		if( isset( $this->$library_name ) )
		{
			return $this->$library_name;
		}

		// No:
		if( file_exists( $this->sys_path . 'libraries/lib_' . $library_name . '.php' ) )
		{
			// The library file exists, load it:
			require_once( $this->sys_path . 'libraries/lib_' . $library_name . '.php' );

			// Make the class name:
			$class_name = 'ineo_lib_' . $library_name;

			// Does the class exist?
			if( class_exists( $class_name ) )
			{
				// Create and init the object:
				$this->$library_name       = new $class_name($this);
				
				// Make a reference for the old-style:
				$this->libs[$library_name] =& $this->$library_name;
				
				// Return it:
				return $this->libs[$library_name];
			}
			else
			{
				// Class does not exist:
				$this->error = 'error_load_library';
				$this->error();
			}
		}
		else
		{
			// The library file does not exist, something's up - let's error:
			$this->error = 'error_load_library';
			$this->error();
		}
	}

	/**
	* Load module file ( stored in /modules )
	*
	* @todo Add error message 'error_load_module'
	*/
	function load_module( $module_name )
	{
		$module_name = preg_replace('/([^a-zA-Z0-9_]+)/','',$module_name);

		// dirty ho hack:
		$module_name = ($module_name == 'xml') ? 'dynamic' : $module_name;

		// Have we loaded it already?
		if( isset( $this->modules[$module_name] ) )
		{
			return $this->modules[$module_name];
		}

		// No:
		if( file_exists( $this->sys_path . 'modules/mod_' . $module_name . '.php' ) )
		{
			// The module file exists, load it:
			require_once( $this->sys_path . 'modules/mod_' . $module_name . '.php' );

			// Make the class name:
			$class_name = 'ineo_mod_' . $module_name;

			// Does the class exist?
			if( class_exists( $class_name ) )
			{
				// Yes - Create an instance:
				$this->active_module = new $class_name();

				// Pass this class & ipsclass to it:
				$this->active_module->ipsclass =& $this->ipsclass;
				$this->active_module->core     =& $this;
				
				// Initialize:
				$this->active_module->module_init();
			}
			else
			{
				// Class does not exist:
				$this->error = 'error_load_module';
				$this->error();
			}
		}
		else
		{
			// The module file does not exist, something's up - let's error:
			$this->error = 'error_load_module';
			$this->error();
		}
	}

	/**
	* Do output - Final call back to the calling component to finalize and display.
	*/
	function do_output()
	{
		$this->api->nav = $this->nav;
		
		if( $this->ipsclass->vars['ineo_qs_box'] )
		{
			$search_box = $this->templates['skin_ineo_global']->quick_search_box();
		}
		else
		{
			$search_box = '';
		}
				
		
		
		// Set up this object for passing to the template:
		$this->menu  = $this->templates['skin_ineo_global']->global_menu($search_box,$this->menu);
		$this->menu .= $this->sidebar;
		$this->js_and_css = $this->templates['skin_ineo_global']->global_js_and_css();
		$this->copyright  = $this->copyright();
		$this->search_box = $search_box;
		
		if( empty($this->no_wrapper) )
		{
			$this->out = $this->templates['skin_ineo_global']->global_wrapper($this);
		}
		
		// Replace &ineo; with ineo_page_title
		$this->out =& str_replace('&ineo;',$this->ipsclass->vars['ineo_page_title'],$this->out);
		
		// Send the output:
		$this->api->do_output( $this->out );
	}

	/**
	* Call the API function to load a template group for use:
	*
	* Also doubles as a loading function for $this->templates[], 
	* which will allow it to access ( IPB EG: $this->ipsclass->compiled_templates[] )
	*/
	function load_template( $group )
	{
		$groups = array( 'categories' => 'skin_ineo_categories', 'articles' => 'skin_ineo_articles', 'global' => 'skin_ineo_global', 'mod' => 'skin_ineo_mod' );

		if( isset( $groups[$group] ) )
		{
			$load = $groups[$group];
			$alias = $group;
		}

		$this->api->load_template( $load );

		if( !isset( $this->templates[$alias] ) )
		{
			$this->templates[$alias] =& $this->templates[$load];
		}
	}


	/**
	* Figure out what the incoming call is, where we're going and what to call.
	*/
	function op_handle()
	{   
		// Get the module id:
		$mod_id = $this->ipsclass->input['mod'];

		// Get the action id:
		$act_id = $this->ipsclass->input['op'];
		$act_id = preg_replace('/([^a-zA-Z0-9_]+)/','',$act_id);

		// Load the module:
		$this->load_module( $mod_id );

		// Run the action:
		$this->active_module->$act_id();
	}

	/**
	* Convert operation shortcuts, like showcat=, etc. to their full versions:
	*/
	function convert_op_shortcuts()
	{
		// Default module is global:
		if( !isset($this->ipsclass->input['mod']) )
		{
			$this->ipsclass->input['mod'] = 'global';
		}
		
		// Default action is overview
		// Updated for 1.1, was previously index.
		if( !isset($this->ipsclass->input['op']) )
		{
			$this->ipsclass->input['op'] = 'overview';
		}

		// Set up show category shortcut:
		if( !empty($this->ipsclass->input['showcat']) )
		{
			$this->ipsclass->input['mod'] = 'category';
			$this->ipsclass->input['op']  = 'cat';
			$this->ipsclass->input['id']  = $this->ipsclass->input['showcat'];
		}

		// Show article shortcut:
		if( !empty($this->ipsclass->input['showarticle']) )
		{
			$this->ipsclass->input['mod'] = 'article';
			$this->ipsclass->input['op']  = 'view';
			$this->ipsclass->input['id']  = $this->ipsclass->input['showarticle'];
		}
		
		// Attachment show shortcut.
		// todo: check if this is obsolete with the ipb attach sys.
		if( !empty($this->ipsclass->input['downloadfile']) )
		{
			$this->ipsclass->input['mod'] = 'article';
			$this->ipsclass->input['op']  = 'attach_download';
			$this->ipsclass->input['id']  = $this->ipsclass->input['downloadfile'];
		}

		// XML is a bit of a stupid module name, considering we don't use XML for our AJAX
		// requests. Renamed the module to dynamic, which is at least somewhat better
		
		// todo: check if this is obsolete yet
		if( $this->ipsclass->input['mod'] == 'xml' )
		{
			$this->ipsclass->input['mod'] = 'dynamic';
		}
		
		$this->module =& $this->ipsclass->input['mod'];
		$this->action =& $this->ipsclass->input['op'];
		$this->id     =& $this->ipsclass->input['id'];
	}

	/**
	* Copyright Display
	*
	* Updated for 1.1, adding copyright removal ability. - Dan
	*/
	function copyright()
	{
		//------------
		// IMPORTANT!
		// -----------
		// REMOVING OR EDITING ANY PART OF THIS FUNCTION IS IN VIOLATION OF YOUR LICENSE
		// AGREEMENT. YOU ARE NOT AUTHORISED TO REMOVE COPYRIGHT OUTPUT FROM YOUR COPY OF
		// INEO WITHOUT PURCHASING COPYRIGHT REMOVAL.
		// 
		// ONCE YOU HAVE DONE SO, WE WILL PROVIDE YOU WITH A NEW VERSION.PHP FILE WHICH WILL
		// REMOVE YOUR COPYRIGHT FOR YOU.
		//------------
		if( !isset($this->version['ineo_cp_remove']) or $this->version['ineo_cp_remove'] == false )
		{
			$copy  = '<div style="text-align: center;">Powered by <a href="http://www.veriea.com/ineo">Ineo</a> v' . $this->version['human'] . ' &copy; 2005-2006, <a href="http://www.veriea.com/">Veriea</a>.</div>';
		}
		else
		{
			$copy = '';
		}

		return $copy;
	}

	//---------------------------------------------------
	// ALIAS FUNCTIONS - OLD FUNCTIONS POINTED TO NEW
	//---------------------------------------------------

	/**
	* Add item to breadcrumb:
	*/
	function nav_add( $title, $link )
	{
		return $this->libs['global']->nav_add( $title, $link );
	}

	/**
	* General Error Handler
	*/
	function ineo_error( $message , $title = 'ineo_gen_error' , $link = null )
	{
		$error = array('title' => $this->ipsclass->lang[$title],'message' => $this->ipsclass->lang[$message],'link'  => $link);

		$this->out .= $this->templates['global']->general_error( $error );
	}
	
	/**
	* General error throwing fun!
	*/
	function error( $message = null, $title = null )
	{
		$this->libs['global']->error( $title, $message );
	}
}

?>